<script setup>
import { ref, onMounted, computed, watch } from 'vue'
import { useRouter, useRoute } from "vue-router"
import { useTagStore } from '@/stores'
import { DataBoard, IceDrink, IceTea, House, Setting, ArrowDown } from "@element-plus/icons-vue"

const { tagMap } = useTagStore()

const router = useRouter()
const route = useRoute()
const routeList = ref([])

const routeKey = computed(() => route.fullPath)

const toggleTag = path => {
  router.push(path)
}

const removeTag = path => {
  delete tagMap[path]
}

const setTagMap = () => {
  const lastIndex = routeList.value.length - 1
  tagMap[routeList.value[lastIndex].path] = routeList.value[lastIndex].meta.title
}

watch(() => route.path, () => {
  routeList.value = []
  routeList.value = route.matched.filter(item => item.meta.title)
  setTagMap()
})

onMounted(() => {
  const matchedArr = router.currentRoute.value.matched
  routeList.value = []
  routeList.value = matchedArr.filter(item => item.meta.title)
  setTagMap()
})

const handleCommand = (command) => {
  if (command === 'logout') {
    router.push('/login')
  }
}
</script>

<template>
  <el-container class="container">
    <el-aside class="aside-container">
      <div class="logo">云商大管家</div>
      <el-scrollbar>
        <el-menu
            background-color="#415a77"
            active-text-color="#ffd04b"
            text-color="#fff"
            :router="true"
            :default-active="$route.path"
            :unique-opened="true"
        >
          <el-menu-item index="/">
            <el-icon><House /></el-icon>
            <span>首页</span>
          </el-menu-item>
          <el-sub-menu index="1">
            <template #title>
              <el-icon><Setting /></el-icon>
              <span>系统管理</span>
            </template>
            <el-menu-item index="1-1">
              <el-icon><IceDrink /></el-icon>
              <span>全局配置</span>
            </el-menu-item>
            <el-menu-item index="/role">
              <el-icon><IceTea /></el-icon>
              <span>角色管理</span>
            </el-menu-item>
            <el-menu-item index="/permission">
              <el-icon><IceTea /></el-icon>
              <span>权限管理</span>
            </el-menu-item>
            <el-menu-item index="/admin">
              <el-icon><IceTea /></el-icon>
              <span>管理员管理</span>
            </el-menu-item>
          </el-sub-menu>
          <el-sub-menu index="2">
            <template #title>
              <el-icon><DataBoard /></el-icon>
              <span>数据管理</span>
            </template>
            <el-menu-item index="2-1">
              <el-icon><IceDrink /></el-icon>
              <span>会员管理</span>
            </el-menu-item>
            <el-menu-item index="2-2">
              <el-icon><IceTea /></el-icon>
              <span>商品分类管理</span>
            </el-menu-item>
            <el-menu-item index="2-3">
              <el-icon><IceTea /></el-icon>
              <span>商品管理</span>
            </el-menu-item>
          </el-sub-menu>
          <el-menu-item index="3-1">
            <el-icon><IceDrink /></el-icon>
            <span>会员管理</span>
          </el-menu-item>
          <el-menu-item index="3-2">
            <el-icon><IceTea /></el-icon>
            <span>商品分类管理</span>
          </el-menu-item>
          <el-menu-item index="3-3">
            <el-icon><IceTea /></el-icon>
            <span>商品管理</span>
          </el-menu-item>
          <el-menu-item index="3-1">
            <el-icon><IceDrink /></el-icon>
            <span>会员管理</span>
          </el-menu-item>
          <el-menu-item index="3-2">
            <el-icon><IceTea /></el-icon>
            <span>商品分类管理</span>
          </el-menu-item>
          <el-menu-item index="3-3">
            <el-icon><IceTea /></el-icon>
            <span>商品管理</span>
          </el-menu-item>
          <el-menu-item index="3-1">
            <el-icon><IceDrink /></el-icon>
            <span>会员管理</span>
          </el-menu-item>
          <el-menu-item index="3-2">
            <el-icon><IceTea /></el-icon>
            <span>商品分类管理</span>
          </el-menu-item>
          <el-menu-item index="3-3">
            <el-icon><IceTea /></el-icon>
            <span>商品管理</span>
          </el-menu-item>
          <el-menu-item index="3-1">
            <el-icon><IceDrink /></el-icon>
            <span>会员管理</span>
          </el-menu-item>
          <el-menu-item index="3-2">
            <el-icon><IceTea /></el-icon>
            <span>商品分类管理</span>
          </el-menu-item>
          <el-menu-item index="3-3">
            <el-icon><IceTea /></el-icon>
            <span>商品管理</span>
          </el-menu-item>
          <el-menu-item index="3-1">
            <el-icon><IceDrink /></el-icon>
            <span>会员管理</span>
          </el-menu-item>
          <el-menu-item index="3-2">
            <el-icon><IceTea /></el-icon>
            <span>商品分类管理</span>
          </el-menu-item>
          <el-menu-item index="3-3">
            <el-icon><IceTea /></el-icon>
            <span>商品管理最</span>
          </el-menu-item>
        </el-menu>
        <div style="height: 80px;"></div>
      </el-scrollbar>
    </el-aside>
    <el-container class="main-container">
      <el-header class="header-container">

        <el-breadcrumb separator=">">
          <el-breadcrumb-item v-for="item in routeList">{{ item.meta.title }}</el-breadcrumb-item>
        </el-breadcrumb>

        <div class="avatar-container">
          <div class="avatar"><el-avatar :size="32" src="https://pic2.zhimg.com/80/v2-939e88045f0c8f57903894d034c07ae9_720w.webp" /></div>
          <div class="dropdown-container">
            <el-dropdown @command="handleCommand">
              <span class="dropdown-info">
                张小明
                <el-icon><arrow-down /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="logout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>

      </el-header>

      <el-tabs
          v-model="$route.path"
          type="card"
          closable
          @tab-change="toggleTag"
          @tab-remove="removeTag"
          v-show="Object.keys(tagMap).length > 0"
      >
        <el-tab-pane
            v-for="(item, index) in tagMap"
            :key="index"
            :label="item"
            :name="index"
        >
        </el-tab-pane>
      </el-tabs>

      <el-main class="content-container">
        <div class="data-container">
          <router-view v-slot="{ Component }">
            <keep-alive>
              <component :is="Component" :key="routeKey" />
            </keep-alive>
          </router-view>
        </div>
      </el-main>
    </el-container>
  </el-container>
</template>

<style scoped lang="scss">
  .container {
    :deep(.el-form-item) {
      margin: {
        bottom: 12px;
      };
    }
    .el-tabs {
      background-color: #fff;
      height: 30px;
      :deep(.is-active) {
        color: var(--el-color-primary);
      }
    }
    :deep(.el-tabs__header) {
      margin-bottom: 0;
      height: 30px;
    }
    :deep(.el-tabs__item) {
      color: #5c5c5c;
      font-size: 12px;
      height: 30px;
    }
    height: 100vh;
    background-color: $main-bg-color;
    .aside-container {
      width: 230px;
      background-color: $theme-color;
      overflow: hidden;
      .el-menu {
        border: none;
      }
      .logo {
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
        height: 60px;
        color: #fff;
        font-size: 16px;
        font-weight: 500;
      }
    }
    .main-container {
      .header-container {
        height: 50px;
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        background-color: #fff;
        .avatar-container {
          display: flex;
          flex-direction: row;
          justify-content: center;
          align-items: center;
          margin-right: 10px;
          .avatar {
            margin-right: 10px;
          }
          .dropdown-container {
            .dropdown-info:focus {
              outline: none;
            }
            .dropdown-info {
              cursor: pointer;
              color: #5c5c5c;
              display: flex;
              font-size: 12px;
              flex-direction: row;
              align-items: center;
            }
          }
        }
      }
      .content-container {
        padding: {
          left: 12px;
          top: 12px;
          bottom: 0;
        }
        .el-scrollbar {
          background-color: #fff;
        }
        .data-container {
          background-color: #fff;
        }
      }
    }
  }
</style>